package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.exception.ExpCode;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TProductCategory;
import com.quwan.entity.TRole;
import com.quwan.manager.controller.dto.MenuZtree;
import com.quwan.manager.service.MenuService;
import com.quwan.manager.service.RoleService;
import com.quwan.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author laozhang
 * @date 2021/9/25 11:47
 */
@Api(tags = "后台角色管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/role")
public class RoleController  extends BaseController {

    private final RoleService roleService;
    private final MenuService menuService;
    @ApiOperation(value = "角色列表分页",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;roleName——角色名称;remarks——备注;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    @PostMapping("roleListPage")
    public ApiResult<PageResult<TRole>> roleListPage(@RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "10") int size,
                                                     @ApiParam(value = "ID") Long id,
                                                     @ApiParam(value = "角色名称") String roleName) {
        PageResult<TRole> res = roleService.getRoleListPage(page, size, id, roleName);
        return ApiResult.success(res);
    }
    @ApiOperation(value = "角色列表",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;roleName——角色名称;remarks——备注;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    @PostMapping("roleList")
    public ApiResult<List<TRole>> roleList(@ApiParam(value = "ID") Long id,
                                           @ApiParam(value = "角色名称") String roleName) {
        List<TRole> res = roleService.getRoleList(id,roleName);
        return ApiResult.success(res);
    }

    @ApiOperation("角色修改")
    @PostMapping("updateRole")
    public ApiResult updateRole(HttpServletRequest request,
                                @ApiParam(value = "id主键") Integer id,
                                @ApiParam(value = "角色名称") String roleName,
                                @ApiParam(value = "备注") String remarks){
        BaseController.TokenInfo tokenInfo = getTokenInfo(request);
        TRole role  = new TRole();
        role.setId(id);
        role.setAddBy(tokenInfo.getUsername());
        role.setRoleName(roleName);
        role.setRemarks(remarks);
        boolean success = roleService.updateRole(role);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
    @PostMapping("addRole")
    @ApiOperation("添加角色")
    public ApiResult addRole(HttpServletRequest request,
                             @ApiParam(value = "角色名称") String roleName,
                             @ApiParam(value = "备注") String remarks){
        BaseController.TokenInfo tokenInfo = getTokenInfo(request);
        TRole role  = new TRole();
        role.setAddBy(tokenInfo.getUsername());
        role.setRoleName(roleName);
        role.setRemarks(remarks);
        role.setAddTime(DateUtils.getNowDate());
        boolean success = roleService.addRole(role);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
    @PostMapping("detailRole")
    @ApiOperation(value = "角色详情",notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;roleName——角色名称;remarks——备注;addBy——增加人;addTime——增加时间;</span>",httpMethod = "POST")
    public ApiResult detailRole(@ApiParam(value = "id主键") @RequestParam(value="id") Integer id){
        TRole role = roleService.detailRole(id);
        return ApiResult.success(role);
    }
    @PostMapping("delRole")
    @ApiOperation("删除角色")
    public ApiResult delRole(@ApiParam(value = "id主键") @RequestParam(value="id") Integer id){
        if(id.equals(1)){
            return ApiResult.error(ExpCode.SYSTEM_NOT_POWER.getMsg());
        }
        boolean success = roleService.delRole(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("getRoleMenu")
    @ApiOperation("获取角色关联菜单——树形结构")
    public ApiResult getRoleMenuList(@ApiParam(value = "角色id") @RequestParam(value = "roleId") Integer roleId){
        List<MenuZtree> ztreeList= menuService.getZtreeMenuListByRoleId(roleId);

        return ApiResult.success(ztreeList);
    }
    @PostMapping("configPower")
    @ApiOperation("分配权限")
    public ApiResult configPower(@ApiParam(value = "角色id") @RequestParam(value = "roleId") Integer roleId,
                                 @ApiParam(value = "菜单id(多个用英文下逗号','隔开)") @RequestParam(value = "menuIds") String menuIds){
        roleService.configPower(roleId,menuIds);
        return ApiResult.successMsg();
    }
}
